package com.dhcc.cdc.nlp.controller;


import java.util.List;
import java.util.Map;


import com.dhcc.cdc.nlp.cache.schemadic.INlpSchemaDicCache;
import com.dhcc.cdc.nlp.cache.schemadic.NlpSchemaDicCache;
import com.dhcc.core.framework.annotion.CacheClear;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.cdc.nlp.entity.NlpSchemaDicEntity;
import com.dhcc.cdc.nlp.service.INlpSchemaDicService;


/**
 * NLP标签字典表
 * 
 * @ClassName: NlpSchemaDicController
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-03-17 08:15:19
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/nlpschemadic")
public class NlpSchemaDicController extends BaseController {

	private String PREFIX = "/backend/sds/dhcnlpschema/";

	@Autowired
	private INlpSchemaDicService nlpSchemaDicService;
	

    /**
     * 跳转到NLP标签字典表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dhcnlpschema.html";
    }
	
    /**
     * 跳转到新增NLP标签字典表
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "dhcnlpschema_add.html";
    }
	
	/**
     * 跳转到修改NLP标签字典表
     */
    @RequestMapping("/to_update/{nlpSchemaDicId}")
    public String toUpdate(@PathVariable String nlpSchemaDicId, Model model) {
        NlpSchemaDicEntity nlpSchemaDic = this.nlpSchemaDicService.selectById(nlpSchemaDicId);
        model.addAttribute("nlpSchemaDic",nlpSchemaDic);
        return PREFIX + "dhcnlpschema_edit.html";
    }

    /**
     * 获取NLP标签字典表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<NlpSchemaDicEntity> page = new PageFactory<NlpSchemaDicEntity>().defaultPage();
        List<NlpSchemaDicEntity> result = this.nlpSchemaDicService.page(page,params);
        page.setRecords((List<NlpSchemaDicEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增NLP标签字典表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(INlpSchemaDicCache.CACHE_NAME)
    public Object add(NlpSchemaDicEntity nlpSchemaDic) {
        if (CommonUtil.isOneEmpty(nlpSchemaDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.nlpSchemaDicService.insert(nlpSchemaDic);
        return R.ok();
    }

    /**
     * 删除NLP标签字典表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(INlpSchemaDicCache.CACHE_NAME)
    public Object delete(@RequestParam Long nlpSchemaDicId) {
	
        this.nlpSchemaDicService.deleteById(nlpSchemaDicId);
        // 如果当前标签还有子标签，一并删除
        this.nlpSchemaDicService.deleteByParentId(nlpSchemaDicId);

        return R.ok();
    }

    /**
     * 修改NLP标签字典表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @CacheClear(INlpSchemaDicCache.CACHE_NAME)
    public Object update(NlpSchemaDicEntity nlpSchemaDic) {
        if (CommonUtil.isOneEmpty(nlpSchemaDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.nlpSchemaDicService.updateById(nlpSchemaDic);
        return R.ok();
    }

    /**
     * 获取标签树
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public Object tree() {
        return this.nlpSchemaDicService.getSchemaTree();
    }
}
